package dolf.objects.weapons.DriveByLaser;

import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.ImageIcon;
import dolf.game.*;
import dolf.objects.weapons.*;
import dolf.objects.*;
import dolf.window.SystemMessagesHandler;

public class DriveByLaser extends CircleObject implements Weapon, java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int Price = 200;
	public static final String Name = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.DriveByLaser.lang.lang").getString("name");
	public static final String WeaponType = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.DriveByLaser.lang.lang").getString("type");
	public static final String Description = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.DriveByLaser.lang.lang").getString("description");;
	public static final javax.swing.ImageIcon Icon = ImageStore.getInstance().getIcon("dolf/objects/weapons/DriveByLaser/gfx/icon.png");
	
	
	private final int MAX_TIMER = 3;
	private final int MAX_BATTERY = 50;
	private final int damage = 1;
	private int battery;
	private int timer;
	private boolean active;
	private Player pl;
	private Point P2;
	private boolean inititalized = false;
	

	public DriveByLaser(){
		super(new Point(0,0),2,true);
		this.setMotion(new Vector(0,0));
		this.setCollitabe(false);
	
	}
	
		
	
	public void initialize(Player _pl){
		if(this.inititalized){
			return;
		}
		this.inititalized = true;
		this.battery = this.MAX_BATTERY;
		this.timer = this.MAX_TIMER;
		this.active = false;
		this.pl = _pl;
		this.pl.setWeaponState();
		Level.getInstance().registerObjectOnTop(this);
	}
	public void deInitialize(){
		Level.getInstance().removeObject(this);
		this.inititalized = false;
		this.pl = null;
	}
	
	public void execute() {
		if(this.active){
			this.P2 = this.GetP2();
		} else {
			if(this.timer == this.MAX_TIMER && Level.getInstance().mousePressed()&&pl.getBall().isMoving()){
				if(this.battery > 0){
					this.active = true;
					this.P2 = this.GetP2();
					this.battery--;
					this.pl.setWeaponState();
					return;
				} else {
					System.err.println("out of Battery!");
				}
			}	
		}
	}

	public void paint(Graphics2D g){
		if(active){
			Color tmp = g.getColor();
			g.setColor(this.pl.getColor());
			g.drawLine(this.pl.getBall().getPosition().getIntX(), this.pl.getBall().getPosition().getIntY(), P2.getIntX(), P2.getIntY());
			g.setColor(tmp);
		}
	}
	
	public ImageIcon getIcon() {
		return Icon;//ImageStore.getInstance().getIcon("dolf/objects/weapons/DriveByLaser/gfx/icon.png");	
	}

	public String getName() {
		return Name;
		/*if (messages == null) loadProperties();
		return messages.getString("name");*/
	}
	
	public String getDescription() {
		return Description;
		/*if (messages == null) loadProperties();
		return messages.getString("description");*/
	}

	public int getPrice() {
		return Price;
	}
	public String getType(){
		return WeaponType;
	}

	public Player getPlayer(){
		return this.pl;
	}
	
	public void remove(){
		Level.getInstance().getObjects().remove(this);
		pl.removeWeapon(this);
		this.pl.setWeaponState();
		this.pl.setAktWeapon(null);
	}
	public double getState(){
		return (double)this.battery/this.MAX_BATTERY;
	}
	
	private Point GetP2(){
		this.setPosition(pl.getBall().getPosition().getCopy());
		this.setMotion( Level.getInstance().getMouse().sub(pl.getBall().getPosition()).setLength(1000));
		double tmprest;
		double rest = 1001;
		WorldObject koll = null;
		for(int i = 0; i < Level.getInstance().getObjects().size(); i++){
			WorldObject obj = Level.getInstance().getObjects().get(i);
			if(obj.isVisible() && obj != this && obj.isCollitabe() && obj != this.pl.getBall() && !obj.isFlat()){
				if((tmprest = this.kollision(obj))>0){
					//if(!(tmprest == rest) || !(obj instanceof LineObject)){
						//kollList.add(obj);
						if(tmprest < rest ){					
							rest = tmprest;
							koll = obj;
						}
					//}
				}
			}
		}
		System.out.println(koll);
		if(koll instanceof Ball){
			Ball obj = (Ball) koll;
			obj.getPlayer().setHP(obj.getPlayer().getHP()-this.damage);
			pl.addDamage(this.damage);
		}
		if(this.timer-- <= 0){
			this.timer = this.MAX_TIMER;
			this.active = false;
		}
		Point tmp = pl.getBall().getPosition().add(this.getMotion().setLength(rest));
		this.setMotion(new Vector(0,0));
		return tmp;
	}
	public boolean isMoving(){
		return false;
	}
	public Point getPosition(){
		return pl.getBall().getPosition();
	}
}
